﻿using HtmlAgilityPack;
using ScrapySharp.Extensions;
using ScrapySharp.Network;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplicationReptile
{
    class Program
    {
        static void Main(string[] args)
        {

            var uri = new Uri("http://www.1399p.com/pk10/");
            var browser1 = new ScrapingBrowser();
            browser1.Encoding = System.Text.Encoding.UTF8;
            var html1 = browser1.DownloadString(uri);
            var doc = new HtmlDocument();
            doc.LoadHtml(html1);
            var html = doc.DocumentNode;

            //foreach (HtmlNode nodeScripte in html.Descendants("script").ToList())
            //{
            //    nodeScripte.Remove();
            //}
            //foreach (HtmlNode nodeStyle in html.Descendants("style").ToList())
            //{
            //    nodeStyle.Remove();
            //}
            //foreach (HtmlNode nodeComment in html.Descendants("//comment()").ToList())
            //{
            //    nodeComment.Remove();
            //}


            //foreach (var script in doc.DocumentNode.Descendants("script").ToArray())
            //{
            //    script.Remove();
            //}
            //foreach (var style in doc.DocumentNode.Descendants("style").ToArray())
            //{
            //    style.Remove();
            //}
            //foreach (var comment in doc.DocumentNode.SelectNodes("//comment()").ToArray())
            //{
            //    comment.Remove();
            //}


            //var title = html.CssSelect("title");
            //foreach (var htmlNode in title)
            //{
            //    // var s = utf8_gb2312(htmlNode.InnerText);

            //    Console.WriteLine(htmlNode.InnerText);
            //}

            //var ps = html.CssSelect("table").CssSelectAncestors("#history");
            //foreach (var htmlNode in ps)
            //{
            //    Console.WriteLine(htmlNode.InnerHtml);
            //}
            //int i = 0;
            //Task task = new Task(() =>
            //{
            //    while (i < 5)
            //    {
            //        Console.WriteLine(i);
            //        i++;
            //    }
            //});
            //task.Start();
            //task.Wait(500);
            //Thread.Sleep(5000);
            //Console.WriteLine("等了一下");
            //Encoding utf = Encoding.UTF8;

            //GetDataFromUrl("http://www.1399p.com/pk10/", ref utf);
            //var task1 = Task.Factory.StartNew(() =>
            //{
            //    Console.WriteLine("工厂创建任务");
            //    Thread.Sleep(1000);
            //    Console.WriteLine("1");
            //});

            //var task2 = new Task(() =>
            //{
            //    Console.WriteLine("构造创建任务");
            //    Thread.Sleep(1000);
            //});
            //task2.Start();

            //var task4= task1.ContinueWith(Task =>
            //{
            //    Console.WriteLine("我等你了");
            //});

            //task4.ContinueWith(Task=> {
            //    Console.WriteLine("1我等你了");

            //});
            // TaskClass.ListWithParallel();
            // TaskClass.ConcurrentBagWithPallel();

            //TaskClass.TestPLinq();
            TaskClass.OrderByTest();
            Console.ReadKey();


        }




        /// <summary>
        /// GB2312转换成UTF8
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string gb2312_utf8(string text)
        {
            //声明字符集   
            System.Text.Encoding utf8, gb2312;
            //gb2312   
            gb2312 = System.Text.Encoding.GetEncoding("gb2312");
            //utf8   
            utf8 = System.Text.Encoding.GetEncoding("utf-8");
            byte[] gb;
            gb = gb2312.GetBytes(text);
            gb = System.Text.Encoding.Convert(gb2312, utf8, gb);
            //返回转换后的字符   
            return utf8.GetString(gb);
        }

        /// <summary>
        /// UTF8转换成GB2312
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string utf8_gb2312(string text)
        {
            //声明字符集   
            System.Text.Encoding utf8, gb2312;
            //utf8   
            utf8 = System.Text.Encoding.GetEncoding("utf-8");
            //gb2312   
            gb2312 = System.Text.Encoding.GetEncoding("gb2312");
            byte[] utf;
            utf = utf8.GetBytes(text);
            utf = System.Text.Encoding.Convert(utf8, gb2312, utf);
            //返回转换后的字符   
            return gb2312.GetString(utf);
        }


        /// <summary>
        /// 函数名称：GetDataFromUrl
        /// 功能说明：获取url指定的网页的源码
        /// 参数：string url用于指定 url
        /// 参数：ref Encoding encode用来获取网页中的字符集编码
        /// </summary>
        public static string GetDataFromUrl(string url, ref Encoding encode)
        {
            string str = string.Empty;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            //设置http头
            request.AllowAutoRedirect = true;
            request.AllowWriteStreamBuffering = true;
            request.Referer = "";
            request.Timeout = 10 * 1000;
            request.UserAgent = "";
            HttpWebResponse response = null;
            response = (HttpWebResponse)request.GetResponse();

            //根据http应答的http头来判断编码
            string characterSet = response.CharacterSet;
            //Encoding encode;
            if (characterSet != "")
            {
                if (characterSet == "ISO-8859-1")
                {
                    characterSet = "gb2312";
                }
                encode = Encoding.GetEncoding(characterSet);
            }
            else
            {
                encode = Encoding.Default;
            }

            //声明一个内存流来保存http应答流
            Stream receiveStream = response.GetResponseStream();
            MemoryStream mStream = new MemoryStream();

            byte[] bf = new byte[255];
            int count = receiveStream.Read(bf, 0, 255);
            while (count > 0)
            {
                mStream.Write(bf, 0, count);
                count = receiveStream.Read(bf, 0, 255);
            }
            receiveStream.Close();

            mStream.Seek(0, SeekOrigin.Begin);

            //从内存流里读取字符串
            StreamReader reader = new StreamReader(mStream, encode);
            char[] buffer = new char[1024];
            count = reader.Read(buffer, 0, 1024);
            while (count > 0)
            {
                str += new String(buffer, 0, count);
                count = reader.Read(buffer, 0, 1024);
            }

            //从解析出的字符串里判断charset，如果和http应答的编码不一直
            //那么以页面声明的为准，再次从内存流里重新读取文本
            Regex reg =
               new Regex(@"<meta[\s\S]+?charset=(.*?)""[\s\S]+?>",
                          RegexOptions.Multiline | RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(str);
            if (mc.Count > 0)
            {
                string tempCharSet = mc[0].Result("$1");
                if (string.Compare(tempCharSet, characterSet, true) != 0)
                {
                    encode = Encoding.GetEncoding(tempCharSet);
                    str = string.Empty;
                    mStream.Seek(0, SeekOrigin.Begin);
                    reader = new StreamReader(mStream, encode);
                    buffer = new char[255];
                    count = reader.Read(buffer, 0, 255);
                    while (count > 0)
                    {
                        str += new String(buffer, 0, count);
                        count = reader.Read(buffer, 0, 255);
                    }
                }
            }
            reader.Close();
            mStream.Close();
            if (response != null)
                response.Close();

            return str;

        }



    }

    class t
    {
        public void Food(int i)
        {
            lock (this)
            {
                if (i > 10)
                {
                    i--;
                    Food(i);
                }
            }

        }
    }
}
